www.gusucode.com > VC++ Flash(SWF)文件创建生成Lib库源码+demo-源码程序 > VC++ Flash(SWF)文件创建生成Lib库源码+demo-源码程序/code/SWFLIB_Library/SWFRectangle.cpp

    // SWFRectangle.cpp: implementation of the CSWFRectangle class.
//
//////////////////////////////////////////////////////////////////////

#include "SWFRectangle.h"


CSWFRectangle::CSWFRectangle()
{
	// Init members
	m_SWFRectangle.Nbits = 0;
	m_SWFRectangle.Xmin = m_SWFRectangle.Ymin = 0;
	m_SWFRectangle.Xmax = m_SWFRectangle.Ymax = 100;
	m_SWFStream = NULL;
	m_SWFStreamLength = 0;
}

CSWFRectangle::~CSWFRectangle()
{
	if (m_SWFStream != NULL)
	{
		delete m_SWFStream;
		m_SWFStream = NULL;
	}
}

void CSWFRectangle::SetRectangle(RECT_F rectangle)
{
	// Copy rectangle
	memcpy(&m_Rectangle, &rectangle, sizeof(RECT_F));

	m_SWFRectangle.Xmin = (SHORT)(rectangle.left * 20);
	m_SWFRectangle.Xmax = (SHORT)(rectangle.right * 20);
	m_SWFRectangle.Ymin = (SHORT)(rectangle.top * 20);
	m_SWFRectangle.Ymax = (SHORT)(rectangle.bottom * 20);
}

void CSWFRectangle::SetRectangle(POINT_F location, SIZE_F size)
{
	// Copy rectangle
	RECT_F rectangle = {location.x, location.y, (location.x+size.cx), (location.y+size.cy)};
	memcpy(&m_Rectangle, &rectangle, sizeof(RECT_F));

	m_SWFRectangle.Xmin = (SHORT)(location.x * 20);
	m_SWFRectangle.Ymin = (SHORT)(location.y * 20);
	m_SWFRectangle.Xmax = (SHORT)((location.x + size.cx) * 20);
	m_SWFRectangle.Ymax = (SHORT)((location.y + size.cy) * 20);
}

void CSWFRectangle::GetRectangle(RECT_F& rectangle)
{
	// Copy rectangle
	memcpy(&rectangle, &m_Rectangle, sizeof(RECT_F));
}

UCHAR* CSWFRectangle::BuildSWFStream()
{
	int maxValue = max(max(m_SWFRectangle.Xmin, m_SWFRectangle.Xmax), max(m_SWFRectangle.Ymin, m_SWFRectangle.Ymax));
	UCHAR bitsNeaded = 0;
	int byteIndex=0, bitOffset=0, i;
	USHORT xMin, xMax, yMin, yMax, mask, nextWord;

	// Calculate bits neaded
	while (pow(2, bitsNeaded) < maxValue)
		bitsNeaded++;
	bitsNeaded++;

	m_SWFRectangle.Nbits = bitsNeaded;
	xMin = m_SWFRectangle.Xmin << (16-bitsNeaded);
	xMax = m_SWFRectangle.Xmax << (16-bitsNeaded);
	yMin = m_SWFRectangle.Ymin << (16-bitsNeaded);
	yMax = m_SWFRectangle.Ymax << (16-bitsNeaded);

	// Create byte field
	m_SWFStreamLength = (4*bitsNeaded + 5) / 8;
	if ((4*bitsNeaded + 5) % 8 != 0)
		m_SWFStreamLength++;
	if (m_SWFStream != NULL)
		delete m_SWFStream;
	m_SWFStream = new UCHAR[m_SWFStreamLength];
	memset(m_SWFStream, 0, m_SWFStreamLength);

	// Set bits neaded
	m_SWFStream[byteIndex] = (bitsNeaded << 3);
	bitOffset = 5;

	// Set xMin
	mask = 0x8000;
	for (i=0; i<bitsNeaded; i++)
	{
		nextWord = (mask & xMin) >> (15-i);
		nextWord = nextWord << ((byteIndex+1)*8-bitOffset-1);
		m_SWFStream[byteIndex] |= LOBYTE(nextWord);

		bitOffset++;
		if ((bitOffset % 8) == 0)
			byteIndex++;
		mask = mask >> 1;
	}

	// Set xMax
	mask = 0x8000;
	for (i=0; i<bitsNeaded; i++)
	{
		nextWord = (mask & xMax) >> (15-i);
		nextWord = nextWord << ((byteIndex+1)*8-bitOffset-1);
		m_SWFStream[byteIndex] |= LOBYTE(nextWord);

		bitOffset++;
		if ((bitOffset % 8) == 0)
			byteIndex++;
		mask = mask >> 1;
	}

	// Set yMin
	mask = 0x8000;
	for (i=0; i<bitsNeaded; i++)
	{
		nextWord = (mask & yMin) >> (15-i);
		nextWord = nextWord << ((byteIndex+1)*8-bitOffset-1);
		m_SWFStream[byteIndex] |= LOBYTE(nextWord);

		bitOffset++;
		if ((bitOffset % 8) == 0)
			byteIndex++;
		mask = mask >> 1;
	}

	// Set yMax
	mask = 0x8000;
	for (i=0; i<bitsNeaded; i++)
	{
		nextWord = (mask & yMax) >> (15-i);
		nextWord = nextWord << ((byteIndex+1)*8-bitOffset-1);
		m_SWFStream[byteIndex] |= LOBYTE(nextWord);
		
		bitOffset++;
		if ((bitOffset % 8) == 0)
			byteIndex++;
		mask = mask >> 1;
	}

	return m_SWFStream;
}

int CSWFRectangle::GetSWFStreamLength()
{
	return m_SWFStreamLength;
}